//
//  MoneyDetailViewController.m
//  Findif
//
//  Created by Mac on 2018/11/1.
//  Copyright © 2018年 Cicel Chen. All rights reserved.
//

#import "MoneyDetailViewController.h"
#import "MoneyDetailsTableViewCell.h"
#import "MoneyDetailsModel.h"
#import "BillDetailViewController.h"
#import "ShopBillDetailViewController.h"

@interface MoneyDetailViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, weak)UITableView * tableView;
@property (nonatomic, weak)UILabel * showLabel;
@property (nonatomic, strong)NSArray <MoneyDetailsModel *> * detailArray;

@end

@implementation MoneyDetailViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self loadData];
    [self setupNav];
    [self setupUI];
}


- (void)loadData{
    
    if ([self.type isEqualToString:@"2"]) {
       
        [[ZRNetworkTool sharedTool] getMerchantsBillListsResultWithBlock:^(BOOL success, id responseData) {
            
            if (success) {
                
                self.detailArray = [NSArray yy_modelArrayWithClass:[MoneyDetailsModel class] json:responseData[@"data"]];
            }
        }];
    }else if ([self.type isEqualToString:@"1"]){
        
        [[ZRNetworkTool sharedTool] getUserBillListsResultWithBlock:^(BOOL success, id responseData) {
            
            if (success) {
                
                self.detailArray = [NSArray yy_modelArrayWithClass:[MoneyDetailsModel class] json:responseData[@"data"]];
            }
        }];
    }
    
    
}


- (void)setDetailArray:(NSArray<MoneyDetailsModel *> *)detailArray{
    
    _detailArray = detailArray;
    
    [self.tableView reloadData];
}

- (void)setupNav{
    
    self.title = @"收支明细";
    
//    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back(1)"] style:UIBarButtonItemStylePlain target:nil action:nil];
//
//    self.navigationItem.backBarButtonItem = backItem;
    
//    UIButton * backButton = [[UIButton alloc] init];
//
//    [backButton setImage:[UIImage imageNamed:@"back(1)"] forState:UIControlStateNormal];
//    [backButton addTarget:self action:@selector(backToAccountView) forControlEvents:UIControlEventTouchUpInside];
//
//    UIBarButtonItem * leftButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
//
//    self.navigationItem.leftBarButtonItem = leftButton;
    
//    UIBarButtonItem *backBtn = [[UIBarButtonItem alloc] init];
//    backBtn.title = @"";
//    self.navigationItem.backBarButtonItem = backBtn;
}

- (void)backToAccountView{
    
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)setupUI{
    
    self.view.backgroundColor = ZRWhiteColor;
    
    UITableView * tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = ColorWithRGB(0xF4F5F6);
        tableView.rowHeight = 75 * SCREENH6;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.tableView = tableView;
    
    [self.tableView registerClass:[MoneyDetailsTableViewCell class] forCellReuseIdentifier:@"MoneyDetailsTableViewCell"];
    
    [self.view addSubview:self.tableView];
    
    UILabel * showLabel = [UILabel new];
    
    showLabel.text = @"暂无明细";
    showLabel.textColor = ZRBlackColor;
    showLabel.font = [UIFont systemFontOfSize:17 * SCREENH6];
    showLabel.hidden = YES;
    
    self.showLabel = showLabel;
    
    [self.view addSubview:self.showLabel];
}


- (void)viewDidLayoutSubviews{
    
    [self.showLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.tableView);
        make.centerY.mas_equalTo(self.tableView);
    }];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    //    NSLog(@"=====%lu",(unsigned long)self.goodlistArray.count);
    if (self.detailArray.count == 0) {
        
        self.showLabel.hidden = NO;
    }else{
        
        self.showLabel.hidden = YES;
    }
    return self.detailArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MoneyDetailsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MoneyDetailsTableViewCell" forIndexPath:indexPath];
    
        cell.model = self.detailArray[indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    
    
    HiddenBar;
    
    MoneyDetailsModel * model = self.detailArray[indexPath.row];
    
    if ([self.type isEqualToString:@"1"]) {
        
        BillDetailViewController * billVC = [BillDetailViewController new];
        
        billVC.ID = [NSString stringWithFormat:@"%@",model.id];
        
        [self.navigationController pushViewController:billVC animated:YES];
    }else{
        
        ShopBillDetailViewController * billVC = [ShopBillDetailViewController new];
        
        billVC.ID = [NSString stringWithFormat:@"%@",model.id];
        
        [self.navigationController pushViewController:billVC animated:YES];
    }
    
    
}

@end
